var path = "/OA";

$(function(){
	
	console.log("hello");
	$("#btn").click(function(){
		$.ajax({
			url :path + "/department/addDepartment",
			data:{"name":$("#name").val(),"description" : $("#description").val(),"parentid":$("#parentid").val()},
			dataType:"json",
			error:function(){
				layer.alert("权限不足");
			},
			success:function(data){
				console.log(data);
				if(data.success){
					layer.alert(data.message,'信息提示',function(){
						location.reload();
					});
				}else{
					layer.alert(data.message);
					console.log(data);
				}
			}
		});
	});
	
});

function returnDepartment(departmentid){
	console.log(departmentid);
	$.ajax({
		url:path +"/department/getDepartment",
		data:{"departmentid":departmentid},
		dataType:"json",
		error:function(){
			layer.alert("系统错误");
		},
		success:function(data){
			if(data.success){
				$("#ename").val(data.object.name);
				$("#edescription").val(data.object.description);
				$("#departmentid").val(data.object.id);
				$("#parentid").val(data.object.parentid);
			}
		}
	});
}

$("#updateBtn").click(function(){
	$.ajax({
		url :path + "/department/updateDepartment",
		type:"post",
		data:{"name":$("#ename").val(),"description" : $("#edescription").val() ,"id" : $("#departmentid").val(),"parentid":$("#parentid").val()},
		dataType:"json",
		error:function(){
			layer.alert("权限不足");
		},
		success:function(data){
			console.log(data);
			if(data.success){
				layer.alert(data.message,'信息提示',function(){
					location.reload();
				});
			}else{
				layer.alert(data.message);
				console.log(data);
			}
		}
	});
})

function sureDelete(id){
	layer.confirm("是否删除该部门?" ,{
		btn:['是','否']
	},function(){
		$.ajax({
			url : path +"/department/deleteDepartment",
			data:{"id" : id},
			dataType:"json",
			error:function(){
				layer.alert("权限不足");
			},
			success:function(data){
				if(data.success){
					layer.alert(data.message,'信息提示',function(){
						location.reload();
					});
				}else{
					layer.alert(data.message);
					console.log(data);
				}
			}
		});
	}
	)
}
